const { prisma } = require('../config/prisma');

/**
 * 获取系统配置
 */
const getSystemConfig = async (req, res) => {
    try {
        const configs = await prisma.systemConfig.findMany();
        
        // 转换配置为对象格式
        const configObject = {
            share: {},
            member: {
                prices: []
            },
            points: {}
        };

        configs.forEach(config => {
            try {
                const value = JSON.parse(config.value);
                if (config.key.startsWith('share.')) {
                    const key = config.key.replace('share.', '');
                    configObject.share[key] = value;
                } else if (config.key.startsWith('member.')) {
                    const key = config.key.replace('member.', '');
                    configObject.member[key] = value;
                } else if (config.key.startsWith('points.')) {
                    const key = config.key.replace('points.', '');
                    configObject.points[key] = value;
                }
            } catch (e) {
                console.error('Parse config value error:', e);
            }
        });

        res.json({
            code: 0,
            data: configObject
        });
    } catch (error) {
        console.error('Get system config error:', error);
        res.status(500).json({
            code: 500,
            message: '获取系统配置失败'
        });
    }
};

module.exports = {
    getSystemConfig
}; 